package com.robot.agent.controller;

import com.alibaba.fastjson.JSON;
import com.robot.agent.client.OrdersClient;
import com.robot.agent.pojo.Agent;
import com.robot.agent.pojo.client.Purchase;
import com.robot.agent.service.AgentService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * purchase控制器层
 *
 * @author lin
 */
@RestController
@CrossOrigin
@RequestMapping("/purchase")
public class PurchaseController {
    @Autowired
    private AgentService agentService;

    @Autowired
    private OrdersClient ordersClient;

    @Autowired
    private HttpServletRequest request;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     * agent表status状态 0-禁用 1-启用
     */
    private String agentStatusEnable = "1";

    /**
     * 通过agentId查询purchase列表并分页
     *
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "{page}/{size}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public Result findPurchases(@PathVariable int page, @PathVariable int size) {
        Claims claims = (Claims) request.getAttribute("robot_agent");
        if (claims == null) {
            return new Result(StatusCode.ERROR_TOKEN_EXPIRED);
        }
        logger.info("claims.getId()=" + claims.getId());
        //前端调用findByToken接口检查过了,所以这里不再对代理商是否存在和状态进行判断

        return new Result(StatusCode.SUCCESS_QUERY, JSON.parseObject(ordersClient.thirdFindPurchasesByAgentIdAndDeletedAtIsNullOrderByCreatedAtDesc(claims.getId(), page, size)));
    }

    /**
     * 通过agentId和status查询purchase列表并分页
     *
     * @param status
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "{status}/{page}/{size}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public Result findPurchases(@PathVariable String status, @PathVariable int page, @PathVariable int size) {
        Claims claims = (Claims) request.getAttribute("robot_agent");
        if (claims == null) {
            return new Result(StatusCode.ERROR_TOKEN_EXPIRED);
        }
        logger.info("claims.getId()=" + claims.getId());
        //前端调用findByToken接口检查过了,所以这里不再对代理商是否存在和状态进行判断

        return new Result(StatusCode.SUCCESS_QUERY, JSON.parseObject(ordersClient.thirdFindPurchasesByAgentIdAndStatusAndDeletedAtIsNullOrderByCreatedAtDesc(claims.getId(), status, page, size)));
    }

    /**
     * 通过agentId和id查询purchase列表并分页
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public Result findPurchase(@PathVariable String id) {
        Claims claims = (Claims) request.getAttribute("robot_agent");
        if (claims == null) {
            return new Result(StatusCode.ERROR_TOKEN_EXPIRED);
        }
        logger.info("claims.getId()=" + claims.getId() + ", purchase's id=" + id);
        //前端调用findByToken接口检查过了,所以这里不再对代理商是否存在和状态进行判断

        Purchase purchaseInDb = JSON.parseObject(ordersClient.thirdFindPurchaseByIdAndAgentId(id, claims.getId()), Purchase.class);
        logger.info("purchaseInDb="+purchaseInDb);
        if (purchaseInDb == null) {
            return new Result(StatusCode.ERROR_RECORD_DOES_NOTE_EXIST);
        }
        return new Result(StatusCode.SUCCESS_QUERY, purchaseInDb);
    }
}
